﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZK.BLL;
using ZK.Entity;
using ZK.MyControl;

namespace ZK.Client.WinForm
{
    public partial class FrmVDJTemplate : Form
    {
        public FrmVDJTemplate()
        {
            InitializeComponent();
        }
        BindingList<VDJTemplate> bindinglist = new BindingList<VDJTemplate>();
        BindingSource bindingSource = new BindingSource();
        private void FrmVDJTemplate_Load(object sender, EventArgs e)
        {
            bindingSource.DataSource = bindinglist;
            dataView1.DataSource = bindingSource;
            AddCloumns();
            LoadData();
        }



        private void AddCloumns()
        {
            dataView1.Columns.Add(DataGridColumnHelper.TextBoxColumnCreate(typeof(VDJTemplate), nameof(VDJTemplate.TemplateName), true));
            dataView1.Columns.Add(DataGridColumnHelper.TextBoxColumnCreate(typeof(VDJTemplate), nameof(VDJTemplate.Delay), true));
            dataView1.Columns.Add(DataGridColumnHelper.TextBoxColumnCreate(typeof(VDJTemplate), nameof(VDJTemplate.CharacterGap), true));
            dataView1.Columns.Add(DataGridColumnHelper.TextBoxColumnCreate(typeof(VDJTemplate), nameof(VDJTemplate.CharWidth), true));
            dataView1.Columns.Add(DataGridColumnHelper.TextBoxColumnCreate(typeof(VDJTemplate), nameof(VDJTemplate.CharHeight), true));
            dataView1.Columns.Add(DataGridColumnHelper.TextBoxColumnCreate(typeof(VDJTemplate), nameof(VDJTemplate.FontHigh), true));
        }


        private void LoadData()
        {
            var list = VDJTemplateBLL.Instance.GetList();
            bindinglist.Clear();
            if (list != null)
            {
                foreach (var item in list)
                {
                    bindinglist.Add(item);
                }
            }

        }



        private void btnDel_Click(object sender, EventArgs e)
        {
            if (dataView1.CurrentCell != null && dataView1.CurrentCell.RowIndex > -1)
            {
                if (MessageBox.Show("确定要删除?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {

                    if (VDJTemplateBLL.Instance.DeleteById(bindinglist[dataView1.CurrentCell.RowIndex].Id))
                    {
                        bindinglist.RemoveAt(dataView1.CurrentCell.RowIndex);
                    }
                }

            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            FrmVDJTemplateEdit frm = new FrmVDJTemplateEdit();
            if (frm.ShowDialog() == DialogResult.OK)
            {
                LoadData();
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (dataView1.CurrentCell != null && dataView1.CurrentCell.RowIndex > -1)
            {
                FrmVDJTemplateEdit frm = new FrmVDJTemplateEdit();
                frm.VDJTemplate = bindinglist[dataView1.CurrentCell.RowIndex];
                if (frm.ShowDialog() == DialogResult.OK)
                {
                    LoadData();
                }
            }
        }
    }

}
